package com.bd.module;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;

import com.bd.framework.pojo.IniInfo;
import com.bd.framework.service.InfoMachCompare;
import com.bd.framework.utils.IniConfig;
import com.bd.module.login.ui.LoginJDialog;
import com.bd.module.login.ui.PwdChangePanel;
import com.bd.module.postgood.ui.PostGoodPanel;
import com.bd.module.querygood.ui.QueryGoodPanel;
import com.bd.module.returngood.ui.ReturnedGoodPanel;
import com.bd.module.returnos.ui.ReturnedOSPanel;
import com.bd.module.salegood.ui.CollectionPanel;
import com.bd.module.salegood.ui.SalseGoodPanel;
/**
 * @author hnxdyl@hotmail.com
 * pos收银主界面
 * */
public class BaseFrame extends JFrame {
	/**
	 */
	private static final long serialVersionUID = 1L;
	/**获得当前屏幕分辨率宽度*/
	private 	double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
	/**获得当前屏幕分辨率高度*/
	private double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
	private JPanel mainPanl;
	/**CardLayout布局管理器*/
	private static CardLayout baseCard = null; 
	/**basePanel,存放其他功能行面板*/
	private static JPanel basePanel;
	/**salsePanel,销售功能面板,及销售面板所包含的组件*/
	private JPanel salseGoodPanel;
	private JPanel collectionPanel;
	private static JPanel postGodPanel;
	private static String panelName;
	/**密码修改界面*/
	private static JPanel pwdChangePanel;
	/**商品查询界面*/
	private static JPanel queryGoodPanel;
	/**返回界面(返回到系统退出等功能界面)*/
	private static JPanel returnedOSPanel;
	/**退货界面*/
	private static JPanel returnedGoodPanel;
	static private BaseFrame bf;
	static  public   BaseFrame getBaseFrame(){
		if(bf==null){
			bf=new BaseFrame();
		}
		return bf;
	}
	
	public  BaseFrame() {	
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭时主界面线程自动关闭
		this.setResizable(false);
		getContentPane().setLayout(new BorderLayout(0, 0));

		basePanel = new JPanel();
		getContentPane().add(basePanel);
		baseCard = new CardLayout(0, 0);
		basePanel.setLayout(baseCard);
		//用于登陆界面时全局遮挡
		JPanel panel_6 = new JPanel();
		basePanel.add(panel_6, "name_7787076343437");
		
		mainPanl = new MainPanl();
		basePanel.add(mainPanl, "functionMainPanl");
		
		//销售面板
		salseGoodPanel = new SalseGoodPanel();
		basePanel.add(salseGoodPanel, "salseFunctionPanel");
		
		collectionPanel =new CollectionPanel();
		basePanel.add(collectionPanel,"collectionPanel");
		
		postGodPanel=new PostGoodPanel();
		basePanel.add(postGodPanel,"postGodFunctionPanel");
	
		/**
		 * 修改密码界面大小,确定中心点,然后根据中心点向外扩展输入框和标签之和大小
		 * */
		pwdChangePanel = new PwdChangePanel();
		basePanel.add(pwdChangePanel, "pwdChangeFunctionPanel");
		
		queryGoodPanel = new QueryGoodPanel();
		basePanel.add(queryGoodPanel, "queryGoodFunctionPanel");
	
		returnedOSPanel = new ReturnedOSPanel();
		basePanel.add(returnedOSPanel, "returnedOSFunctionPanel");
		
		returnedGoodPanel = new ReturnedGoodPanel();
		basePanel.add(returnedGoodPanel, "returnedGoodFunctionPanel");
		
		JPanel collectionPanel = new CollectionPanel();
		basePanel.add(collectionPanel, "name_44095116066291");
		
		//设置主界面的title(如何居中),或者使用中间panel
		//Border titleBorder1=BorderFactory.createTitledBorder("Robot Control");      
		this.addKeyListener( new KeyMonitor());
	}
	
	private class KeyMonitor extends KeyAdapter{
		public void keyReleased(KeyEvent keyEvent) {
			if(keyEvent.getKeyCode() ==  KeyEvent.VK_S && keyEvent.isAltDown()) {
        		panelName="salseGoodPanel";
        		baseCard.show(basePanel, "salseFunctionPanel");
        	}
        	//处理Alt+T组合键,弹出退货界面
        	if(keyEvent.getKeyCode() ==  KeyEvent.VK_T && keyEvent.isAltDown()) {
        		panelName="returnedGoodPanel";
        		baseCard.show(basePanel, "returnedGoodFunctionPanel");
        	}
        	//处理Alt+Q组合键,弹出查询界面
        	if(keyEvent.getKeyCode() ==  KeyEvent.VK_Q && keyEvent.isAltDown()) {
        		panelName="queryGoodPanel";
        		baseCard.show(basePanel, "queryGoodFunctionPanel");
        	}
        	//处理Alt+M组合键,弹出修改密码界面
        	if(keyEvent.getKeyCode() ==  KeyEvent.VK_M && keyEvent.isAltDown()) {
        		panelName="pwdChangePanel";
        		baseCard.show(basePanel, "pwdChangeFunctionPanel");
        		PwdChangePanel.getPassPanel().getOldPass().requestFocus();
        	}
        	//处理Alt+P组合键,弹出过货界面
        	if(keyEvent.getKeyCode() ==  KeyEvent.VK_P && keyEvent.isAltDown()) {
        		panelName="postGodPanel";
        		baseCard.show(basePanel, "postGodFunctionPanel");
        	}
        	//处理Alt+R组合键,弹出返回界面
        	if(keyEvent.getKeyCode() ==  KeyEvent.VK_R && keyEvent.isAltDown()) {
        		panelName="returnedOSPanel";
        		baseCard.show(basePanel, "returnedOSFunctionPanel");
        	}
        	//处理Alt+O组合键,弹出主功能界面
        	if(keyEvent.getKeyCode() ==  KeyEvent.VK_O && keyEvent.isAltDown()) {
        		panelName="mainPanl";
        		baseCard.show(basePanel, "functionMainPanl");
        	}
        	//处理salse界面F6按键按下，弹出付款界面
        	if(keyEvent.getKeyCode() ==  KeyEvent.VK_F6) {
        	if(panelName=="salseGoodPanel")
        		baseCard.show(basePanel, "collectionPanel");
        	}
		}
	}
	
	public CardLayout getBaseCard() {
		return baseCard;
	}

	public JPanel getBasePanel() {
		return basePanel;
	}

	public static void main(String[] args){
		//308*214 登陆对话框的大小
		//double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		//double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
	//	BaseFrame baseFrame = new BaseFrame();
		BaseFrame baseFrame=BaseFrame.getBaseFrame();
		baseFrame.setBounds(0, 0, (int)baseFrame.width-1, (int)baseFrame.height-1);
		//baseFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		baseFrame.setUndecorated(true);//隐藏父窗体的边框
		baseFrame.getRootPane().setWindowDecorationStyle(JRootPane.COLOR_CHOOSER_DIALOG);
		//限制Frame关闭按钮失效
		//baseFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		baseFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		baseFrame.setVisible(true);
		//处理登录LoginJDialog的位置大小,且设置其焦点
		LoginJDialog.getLoginJDialog().setBounds((baseFrame.getWidth()-328)/2,(baseFrame.getHeight()-244)/2,356,244);
		//获得ini文件信息
		IniConfig ini = new IniConfig();
		IniInfo iniInfo = null;
		Map<String, Object> map = ini.getIniInfo();
		if (map.get("error") == null || map.get("error").equals(null)) {
			iniInfo = (IniInfo) map.get("success");
		} else {
			JOptionPane.showMessageDialog(null, map.get("读取ini文件失败!"));
		}
		iniInfo.getBackupIp();iniInfo.getMainIp();
		//创建数据连接,更新本地数据
		//
		String flag = InfoMachCompare.compareInfoMach(iniInfo.getMainIp(), iniInfo.getShopCode());
		if(flag.equals("success")){
			LoginJDialog.getLoginJDialog().requestFocus();
			LoginJDialog.getLoginJDialog().setVisible(true);
		}else{
			JOptionPane.showMessageDialog(null, flag);
		}
		
	}
}
